package cn.knight.system.controller;

import cn.knight.basic.annotation.PreAuthorize;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.utils.PageList;
import cn.knight.system.domain.Dictionarydetail;
import cn.knight.system.query.DictionarydetailQuery;
import cn.knight.system.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/detail")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DictionarydetailController {
    
    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * @Title: loadAll
     * @Description: 部门查询所有数据
     * @Author: winnie
     * @Version: 1.0
     * @Date:  24/6/2022 上午11:55
     * @Parameters: []
     * @Return java.util.List<cn.knight.org.domain.Dictionarydetail>
     */
    @GetMapping
    @ApiOperation(value = "查询部门所有数据" )
    @PreAuthorize(name = "数据字典明细查询", sn = "detail:loadAll")
    public List<Dictionarydetail> loadAll(){
        return dictionarydetailService.loadAll();
    }

    /**
     * @Title: loadById
     * @Description: 根据部门主键id查询数据
     * @Author: winnie
     * @Version: 1.0
     * @Date:  24/6/2022 上午11:57
     * @Parameters: [id]
     * @Return cn.knight.org.domain.Dictionarydetail
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )

    public  Dictionarydetail loadById(@ApiParam(name="ID属性",required = true)@PathVariable("id") Long id){
        return dictionarydetailService.loadById(id);
    }

    /**
     * @Title: remove
     * @Description: 根据部门主键id删除数据
     * @Author: winnie
     * @Version: 1.0
     * @Date:  24/6/2022 上午11:57
     * @Parameters: [id]
     * @Return cn.knight.org.domain.Dictionarydetail
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "数据字典明细删除", sn = "detail:remove")
    public AjaxResult remove(@ApiParam(value="ID属性",required = true)@PathVariable("id") Long id){
        try {
            dictionarydetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！！");
        }
    }

    /**
     * @Title: addOrUpt
     * @Description: 新增或修改数据
     * @Author: winnie
     * @Version: 1.0
     * @Date:  24/6/2022 下午12:02
     * @Parameters: [dictionarydetail]
     * @Return cn.knight.basic.utils.AjaxResult
     */
    @PutMapping
    @ApiOperation(value = "新增/修改" )
    @PreAuthorize(name = "数据字典明细新增/修改", sn = "detail:addOrUpt")
    public AjaxResult addOrUpt(@ApiParam(name="部门对象",required = true)@RequestBody Dictionarydetail dictionarydetail){
        try {
            if (dictionarydetail.getId() == null) {
                dictionarydetailService.save(dictionarydetail);
            } else {
                dictionarydetailService.update(dictionarydetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
     * @Title: queryRows
     * @Description: 分页查询+高级查询
     * @Author: winnie
     * @Version: 1.0
     * @Date:  24/6/2022 下午12:11
     * @Parameters: [dictionarydetailQuery]
     * @Return cn.knight.basic.utils.PageList<cn.knight.org.domain.Dictionarydetail>
     */
    @PostMapping
    @ApiOperation(value = "分页查询+高级查询" )
    @PreAuthorize(name = "数据字典明细列表", sn = "detail:queryRows")
    public PageList<Dictionarydetail> queryRows(@ApiParam(name="部门对象",required = true)@RequestBody DictionarydetailQuery dictionarydetailQuery){
        System.out.println(dictionarydetailQuery.getTypeId());
        PageList<Dictionarydetail> queryRows = dictionarydetailService.queryRows(dictionarydetailQuery);

        return queryRows;
    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: winnie
     * @Version: 1.0
     * @Date:  26/6/2022 下午11:30
     * @Parameters: [ids]
     * @Return cn.knight.basic.utils.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "数据字典明细批量删除", sn = "detail:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dictionarydetailService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }


}
